using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Firestarter.DB.Schema.BO;
using Firestarter.DB.Schema.Test.BO.TestUtils;
using Firestarter.DB.Schema.Test.Logic;
using FireStarterModeller.BO;
using Firestarter.DB.Schema.UI;
using FireStarterModeller.Test;
using FireStarterModeller.Test.BO;
using Habanero.Testability;
using NUnit.Framework;

namespace Firestarter.DB.Schema.Test.UI
{
    [TestFixture]
    public class TestChooseObjectTypesWizardStep:TestBase
    {
        
        private static string GetRandomString()
        {
            return RandomValueGen.GetRandomString();
        }
        [Test]
        public void Test_ReverseEngineering()
        {
            //---------------Set up test pack-------------------
            ChooseObjectTypesWizardStep wizardStep = new ChooseObjectTypesWizardStep();
            ReverseEngineering reverseEngineering = new ReverseEngineering{ReverseEngineeringSettings = new ReverseEngineeringSettings()};
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            wizardStep.ReverseEngineering = reverseEngineering;
            //---------------Test Result -----------------------
            Assert.AreSame(reverseEngineering, wizardStep.ReverseEngineering);
            Assert.AreSame(reverseEngineering.ReverseEngineeringSettings, wizardStep.ReverseEngineeringSettingsPanelInfo.BusinessObject);
        }

        [Test]
        public void Test_ReverseEngineering_SetToNull()
        {
            //---------------Set up test pack-------------------
            ChooseObjectTypesWizardStep wizardStep = new ChooseObjectTypesWizardStep();
            wizardStep.ReverseEngineering = new ReverseEngineering { ReverseEngineeringSettings = new ReverseEngineeringSettings() };
            //-------------Assert Preconditions -------------
            Assert.IsNotNull(wizardStep.ReverseEngineering);
            Assert.IsNotNull(wizardStep.ReverseEngineeringSettings);
            Assert.IsNotNull(wizardStep.ReverseEngineeringSettingsPanelInfo.BusinessObject);
            //---------------Execute Test ----------------------
            wizardStep.ReverseEngineering = null;
            //---------------Test Result -----------------------
            Assert.IsNull(wizardStep.ReverseEngineering);
            Assert.IsNull(wizardStep.ReverseEngineeringSettings);
            Assert.IsNull(wizardStep.ReverseEngineeringSettingsPanelInfo.BusinessObject);
        }

        [Test]
        public void Test_ReverseEngineeringSettings_Changed()
        {
            //---------------Set up test pack-------------------
            ChooseObjectTypesWizardStep wizardStep = new ChooseObjectTypesWizardStep();
            ReverseEngineering reverseEngineering = new ReverseEngineering();
            wizardStep.ReverseEngineering = reverseEngineering;
            ReverseEngineeringSettings reverseEngineeringSettings = new ReverseEngineeringSettings();
            //-------------Assert Preconditions -------------
            Assert.IsNotNull(wizardStep.ReverseEngineering);
            Assert.IsNull(wizardStep.ReverseEngineeringSettings);
            Assert.IsNull(wizardStep.ReverseEngineeringSettingsPanelInfo.BusinessObject);
            //---------------Execute Test ----------------------
            reverseEngineering.ReverseEngineeringSettings = reverseEngineeringSettings;
            //---------------Test Result -----------------------
            Assert.AreSame(reverseEngineering, wizardStep.ReverseEngineering);
            Assert.AreSame(reverseEngineeringSettings, wizardStep.ReverseEngineeringSettings);
            Assert.AreSame(reverseEngineeringSettings, wizardStep.ReverseEngineeringSettingsPanelInfo.BusinessObject);
        }

        [Test]
        public void Test_ReverseEngineeringSettings_Changed_ToNull()
        {
            //---------------Set up test pack-------------------
            ChooseObjectTypesWizardStep wizardStep = new ChooseObjectTypesWizardStep();
            ReverseEngineering reverseEngineering = new ReverseEngineering { ReverseEngineeringSettings = new ReverseEngineeringSettings() };
            wizardStep.ReverseEngineering = reverseEngineering;
            //-------------Assert Preconditions -------------
            Assert.IsNotNull(wizardStep.ReverseEngineering);
            Assert.IsNotNull(wizardStep.ReverseEngineeringSettings);
            Assert.IsNotNull(wizardStep.ReverseEngineeringSettingsPanelInfo.BusinessObject);
            //---------------Execute Test ----------------------
            reverseEngineering.ReverseEngineeringSettings = null;
            //---------------Test Result -----------------------
            Assert.AreSame(reverseEngineering, wizardStep.ReverseEngineering);
            Assert.IsNotNull(wizardStep.ReverseEngineering);
            Assert.IsNull(wizardStep.ReverseEngineeringSettings);
            Assert.IsNull(wizardStep.ReverseEngineeringSettingsPanelInfo.BusinessObject);
        }

        [Test]
        public void Test_CancelStep_WhenDirtyReverseEngineeringSettings_ShouldCancelChanges()
        {
            //---------------Set up test pack-------------------
            ChooseObjectTypesWizardStep wizardStep = new ChooseObjectTypesWizardStep();
            ReverseEngineering reverseEngineering = new ReverseEngineering();
            ReverseEngineeringSettings reverseEngineeringSettings = TestUtilsReverseEngineeringSettings.CreateSavedReverseEngineeringSettings();
            reverseEngineering.ReverseEngineeringSettings = reverseEngineeringSettings;
            reverseEngineering.Save();
            reverseEngineeringSettings.Save();
            wizardStep.ReverseEngineering = reverseEngineering;
            reverseEngineeringSettings.TableNameFilter = GetRandomString();
            //-------------Assert Preconditions -------------
            Assert.AreSame(reverseEngineering, wizardStep.ReverseEngineering);
            Assert.AreSame(reverseEngineeringSettings, wizardStep.ReverseEngineeringSettings);
            Assert.AreSame(reverseEngineeringSettings, wizardStep.ReverseEngineeringSettingsPanelInfo.BusinessObject);
            Assert.IsTrue(reverseEngineeringSettings.Status.IsDirty);
            //---------------Execute Test ----------------------
            wizardStep.CancelStep();
            //---------------Test Result -----------------------
            Assert.IsFalse(reverseEngineeringSettings.Status.IsDirty);
        }

        [Test]
        public void Test_CancelStep_WhenNullReverseEngineeringSettings_ShouldDoNothing()
        {
            //---------------Set up test pack-------------------
            ChooseObjectTypesWizardStep wizardStep = new ChooseObjectTypesWizardStep();
            ReverseEngineering reverseEngineering = new ReverseEngineering();
            reverseEngineering.ReverseEngineeringSettings = null;
            wizardStep.ReverseEngineering = reverseEngineering;
            //-------------Assert Preconditions -------------
            Assert.AreSame(reverseEngineering, wizardStep.ReverseEngineering);
            Assert.IsNull(reverseEngineering.ReverseEngineeringSettings);
            Assert.IsNull(wizardStep.ReverseEngineeringSettings);
            //---------------Execute Test ----------------------
            wizardStep.CancelStep();
            //---------------Test Result -----------------------
            Assert.AreSame(reverseEngineering, wizardStep.ReverseEngineering);
            Assert.IsNull(reverseEngineering.ReverseEngineeringSettings);
            Assert.IsNull(wizardStep.ReverseEngineeringSettings);
        }

        [Test]
        public void Test_CancelStep_WhenNullRevEng_ShouldDoNothing()
        {
            //---------------Set up test pack-------------------
            ChooseObjectTypesWizardStep wizardStep = new ChooseObjectTypesWizardStep();
            wizardStep.ReverseEngineering = null;
            //-------------Assert Preconditions -------------
            Assert.IsNull(wizardStep.ReverseEngineering);
            Assert.IsNull(wizardStep.ReverseEngineeringSettings);
            //---------------Execute Test ----------------------
            wizardStep.CancelStep();
            //---------------Test Result -----------------------
            Assert.IsNull(wizardStep.ReverseEngineering);
            Assert.IsNull(wizardStep.ReverseEngineeringSettings);
        }

        [Test]
        public void Test_CancelStep_WhenNewReverseEngineeringSettings_ShouldCancelChanges()
        {
            //---------------Set up test pack-------------------
            ChooseObjectTypesWizardStep wizardStep = new ChooseObjectTypesWizardStep();
            ReverseEngineering reverseEngineering = new ReverseEngineering();
            reverseEngineering.Save();
            ReverseEngineeringSettings reverseEngineeringSettings = new ReverseEngineeringSettings();
            reverseEngineering.ReverseEngineeringSettings = reverseEngineeringSettings;
            wizardStep.ReverseEngineering = reverseEngineering;
            reverseEngineeringSettings.TableNameFilter = GetRandomString();
            //-------------Assert Preconditions -------------
            Assert.AreSame(reverseEngineering, wizardStep.ReverseEngineering);
            Assert.AreSame(reverseEngineeringSettings, wizardStep.ReverseEngineeringSettings);
            Assert.AreSame(reverseEngineeringSettings, wizardStep.ReverseEngineeringSettingsPanelInfo.BusinessObject);
            Assert.IsTrue(reverseEngineeringSettings.Status.IsDirty);
            Assert.IsTrue(reverseEngineeringSettings.Status.IsNew);
            //---------------Execute Test ----------------------
            wizardStep.CancelStep();
            //---------------Test Result -----------------------
            //Assert.IsFalse(reverseEngineeringSettings.Status.IsDirty); This remains dirty because the relationship is not cancelled
            Assert.IsTrue(reverseEngineeringSettings.Status.IsNew);
            Assert.IsNull(reverseEngineeringSettings.TableNameFilter);
        }

        [Test]
        public void Test_CancelStep_WhenUnChangedReverseEngineeringSettings_ShouldDoNothing()
        {
            //---------------Set up test pack-------------------
            ChooseObjectTypesWizardStep wizardStep = new ChooseObjectTypesWizardStep();
            ReverseEngineering reverseEngineering = new ReverseEngineering();
            ReverseEngineeringSettings reverseEngineeringSettings = TestUtilsReverseEngineeringSettings.CreateSavedReverseEngineeringSettings();
            reverseEngineering.ReverseEngineeringSettings = reverseEngineeringSettings;
            reverseEngineeringSettings.Save();
            wizardStep.ReverseEngineering = reverseEngineering;
            //-------------Assert Preconditions -------------
            Assert.AreSame(reverseEngineering, wizardStep.ReverseEngineering);
            Assert.AreSame(reverseEngineeringSettings, wizardStep.ReverseEngineeringSettings);
            Assert.AreSame(reverseEngineeringSettings, wizardStep.ReverseEngineeringSettingsPanelInfo.BusinessObject);
            Assert.IsFalse(reverseEngineeringSettings.Status.IsDirty);
            Assert.IsFalse(reverseEngineeringSettings.Status.IsNew);
            //---------------Execute Test ----------------------
            wizardStep.CancelStep();
            //---------------Test Result -----------------------
            Assert.IsFalse(reverseEngineeringSettings.Status.IsDirty);
            Assert.IsFalse(reverseEngineeringSettings.Status.IsNew);
        }    
    }
}
